package io.gitee.thinkbungee.bean;

import io.gitee.thinkbungee.constant.SatelliteConstant;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.function.Consumer;

/**
 * 响应实体
 *
 * @author chenyichang
 * @date 2022/1/27
 */
@Data
@Builder
public final class ResponseResult implements Serializable {

    /**
     * 响应编码
     */
    private String code;
    /**
     * 响应消息
     */
    private String message;
    /**
     * 响应数据
     */
    private Object data;
    /**
     * 响应参数
     */
    private Object[] args;


    /**
     * 后续同步执行
     *
     * @param consumer 执行方法
     * @param param    执行参数
     * @return
     */
    public ResponseResult then(Consumer<Object> consumer, Object param) {
        consumer.accept(param);
        return this;
    }


    public static ResponseResult success() {
        return ResponseResult.builder()
                .code(SatelliteConstant.HttpCode.SUCCESS_CODE)
                .message("success")
                .build();
    }

    public static ResponseResult success(String code, Object... args) {
        return ResponseResult.builder()
                .code(code)
                .args(args)
                .build();
    }

    public static ResponseResult success(Object data) {
        ResponseResult success = success();
        success.setData(data);
        return success;
    }

    public static ResponseResult success(String code, Object data, Object... args) {

        return ResponseResult.builder()
                .code(code)
                .data(data)
                .args(args)
                .build();
    }

    public static ResponseResult fail() {
        return ResponseResult.builder()
                .code(SatelliteConstant.HttpCode.FAIL_CODE)
                .message("fail")
                .build();
    }

    public static ResponseResult fail(String code, Object... args) {
        return ResponseResult.builder()
                .code(code)
                .args(args)
                .build();
    }

    public static ResponseResult fail(String code, Object data, Object... args) {
        return ResponseResult.builder()
                .code(code)
                .data(data)
                .args(args)
                .build();
    }

}
